package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import vo.Item;
public class ItemDAO {
private DataSource ds;
	
	//싱글톤 바로 작성
	private static ItemDAO dao = new ItemDAO();
	private ItemDAO(){
		System.out.println("DataSource Rent.........");
		//2. JNDI 서비스...로 ds를 찾아옴
		try{
			Context ic = new InitialContext();
			ds=(DataSource)ic.lookup("java:comp/env/jdbc/oracleDB");
			System.out.println("DataSource를 찾아옴...");
		}catch(Exception e){
			System.out.println("DataSource를 찾아오지 못했습니다...");
		}
		
	}
	public static ItemDAO getInstance(){
		return dao;
	}
	public  Connection getConnection() throws SQLException{
		System.out.println("디비연결 성공....");
		return ds.getConnection();		
	}
	public void closeAll(PreparedStatement ps, Connection conn)throws SQLException{
		if(ps!=null) ps.close();
	
		if(conn != null) conn.close();
	}
	public void closeAll(ResultSet rs,PreparedStatement ps, Connection conn)throws SQLException{
		if(rs != null){
			rs.close();
			closeAll(ps, conn);
		}
	}//
	//public ArrayList<E>
	
	//////////////////// 비지니스 로직 //////////////////////////////////////////////////
	
	public ArrayList<Item> getAllItems()throws SQLException{
		ArrayList<Item> list = new ArrayList<Item>();
		Connection conn=  null;
		PreparedStatement ps = null;
		ResultSet rs=  null;
		try{
			ItemDAO dao = ItemDAO.getInstance();
			conn = dao.getConnection();
			
			String query = "SELECT * FROM item";
			System.out.println(query);
			ps = conn.prepareStatement(query);
			rs = ps.executeQuery();
			System.out.println("rs 리턴받음......getAllItems()");
			while(rs.next()){
				list.add(new Item(rs.getInt(1), 
						          rs.getString(2),
						          rs.getInt(3), 
						          rs.getString(4),
						          rs.getString(5),
						          rs.getInt(6)));
			}
		}finally{
			closeAll(rs, ps, conn);
		}
		return list;
	}	
	
	public Item getItem(int itemid)throws SQLException{
		Connection conn=  null;
		PreparedStatement ps = null;
		ResultSet rs=  null;
		Item item = null;
		try{
			conn = getConnection();
			String query = "SELECT * FROM item WHERE item_id=?";
			ps = conn.prepareStatement(query);
			ps.setInt(1, itemid);
			rs = ps.executeQuery();
			if(rs.next()){
				item = new Item(itemid,
							    rs.getString(2), 
							    rs.getInt(3), 
							    rs.getString(4), 
							    rs.getString(5),
							    rs.getInt(6));
			}
			
		}finally{
			closeAll(rs, ps, conn);
		}
		return item;
	}
	
	public boolean updateRecordCount(int itemid) throws SQLException{
		Connection conn=  null;
		PreparedStatement ps = null;
		boolean result = false;
		try{
			conn=  getConnection();
			String query = "UPDATE item SET count= count+1 	WHERE item_id=?";
			ps = conn.prepareStatement(query);
			ps.setInt(1,itemid);
			int row = ps.executeUpdate();
			if(row>0) result=true;
		}finally{
			closeAll(ps, conn);
		}
		return result;
	}
}














